package com.newbingo.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IOperatorDao;
import com.newbingo.entity.Lottery;
import com.newbingo.entity.Member;
import com.newbingo.entity.Operator;
import com.newbingo.util.Pager;

public class OperatorDao extends HibernateDaoSupport implements IOperatorDao {
	private static final Log log = LogFactory.getLog(OperatorDao.class);

	
	/*
	 * 操作员员登录验证，根据用户登录名和密码
	 */
	public Operator findByLogin(String uname, String upwd) {
		log.debug("finding Operator instance by login");
		try {
			Operator operator = null;
			String hql = "from Operator as operator where uname=:uName and upwd=:uPwd";
			Query cri = getSession().createQuery(hql);
			cri.setString("uName", uname);
			cri.setString("uPwd", upwd);
			List<Operator> operators = cri.list();
			if (operators.size() > 0 && operators != null) {
				operator = operators.get(0);
			}
			return operator;
		} catch (RuntimeException re) {
			log.error("find Operator instance failed", re);
			throw re;
		}
	}
	
	
	/*
	 * 删除(non-Javadoc)
	 * 
	 * @see com.newbingo.dao.imple.IOperatorDao#delete(com.newbingo.entity.Operator)
	 */
	public void delete(Operator instance) {
		log.debug("deleting Operator instance");
		try {
			Operator operator = this.findById(instance.getUno());
			getSession().delete(operator);
			log.debug("delete Operator instance successful");
		} catch (RuntimeException re) {
			log.error("delete Operator instance failed", re);
			throw re;
		}
	}

	/*
	 * 根据ID查询(non-Javadoc)
	 * 
	 * @see com.newbingo.dao.imple.IOperatorDao#findById(java.lang.Integer)
	 */
	public Operator findById(Integer id) {
		log.debug("finding Operator instance with ID");
		try {
			Operator instance = (Operator) getSession().get(
					"com.newbingo.entity.Operator", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("find with Id failed", re);
			throw re;
		}
	}

	/*
	 * 查询(non-Javadoc)
	 * 
	 * @see com.newbingo.dao.imple.IOperatorDao#findOperator(int, int,
	 *      java.lang.Integer)
	 */
	public Pager findOperator(int start, int limit, Integer agentId) {
		Pager pager = new Pager();
		Criteria cri = getSession().createCriteria(Operator.class);
		cri.createAlias("agent", "ag");
		cri.add(Expression.eq("ag.uno", agentId));
		cri.setProjection(Projections.rowCount());
		pager.setTotalSize(((Number) cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		List<Operator> list = cri.list();
		pager.setData(list);
		return pager;

	}

	/*
	 * 修改(non-Javadoc)
	 * 
	 * @see com.newbingo.dao.imple.IOperatorDao#merge(com.newbingo.entity.Operator)
	 */
	public Operator merge(Operator instance) {
		log.debug("merging Operator instance");
		try {
			Operator operator = this.findById(instance.getUno());
			Operator instances = (Operator) getSession().merge(instance);
			return instances;
		} catch (RuntimeException re) {
			log.error("merge Operator instance failed", re);
			throw re;
		}
	}

	/*
	 * 保存(non-Javadoc)
	 * 
	 * @see com.newbingo.dao.imple.IOperatorDao#save(com.newbingo.entity.Operator)
	 */
	public void save(Operator instance) {
		log.debug("saving Operator instance");
		try {
			getSession().save(instance);
		} catch (RuntimeException re) {
			log.error("save Operator instance failed", re);
			throw re;
		}

	}

}
